package com.bjpowernode.utils.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.config.context.ApplicationContextProvider;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.service.TDicValueService;

import java.util.Map;

/**
 * 称呼转换器
 */
public class AppellationConverter implements Converter<Integer> {

    /**
     * excel 单元格中的  数据转换为Java对象属性的值
     * @param cellData  :  excel单元格的数据对象
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 获取spring的bean
        TDicValueService tDicValueService = ApplicationContextProvider.getApplicationContext().getBean(TDicValueService.class);
        // 调用方法 ,获取全部数据
        Map<String, Integer> stringIntegerMap = tDicValueService.selectAllNameAndIdByClueState(Constants.APPELLATION);
        // 查询转换数据
        String stringValue = cellData.getStringValue();
        // 转换赋值
        return stringIntegerMap.get(stringValue);
    }

    /**
     * java 数据  ---->  excel
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == 18) {
            return new WriteCellData<>("先生");
        } else {
            return new WriteCellData<>("女士");
        }
    }
}
